
import { BYTEDANCE } from "cc/env";
import { uniConfig } from "../config/UniConfig";
import { Databus } from "../core/Databus";
import { PlatformSetting } from "../platform/PlatformSetting";
const databus = Databus.Instance();
// 更新用户头像和昵称
export function updateUserHeader(userId: string, nickName: string, avatarUrl: string) {
  if (PlatformSetting.isHTML5()) return;
  const url = `${uniConfig.apiUrl}/unigame/user/header`;
  const xhr = new XMLHttpRequest();
  console.log('request url ', url);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
      console.log(xhr.responseType, xhr.response, xhr.responseText);
      var response = JSON.parse(xhr.responseText);
    }
  };
  xhr.open("POST", url, true);
  xhr.setRequestHeader("Content-Type", "application/json");

  const data = {
    user_id: +userId,
    nick_name: nickName,
    avatar_url: avatarUrl,
  };
  xhr.send(JSON.stringify(data));
}

// 更新用户等级信息
export function updateUserPlayLevel(userId: string, playLevel: number, diffLevel: number) {
  if (PlatformSetting.isHTML5()) return;
  const url = `${uniConfig.apiUrl}/unigame/user/level`;
  const xhr = new XMLHttpRequest();
  console.log('request url ', url);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
      console.log(xhr.responseType, xhr.response, xhr.responseText);
      var response = JSON.parse(xhr.responseText);
    }
  };
  xhr.setRequestHeader("Content-Type", "application/json");
  // xhr.open("GET", url, true);
  xhr.open("POST", url, true);
  const data = {
    user_id: +userId,
    play_level: playLevel,
    diff_level: diffLevel,
  };
  xhr.send(JSON.stringify(data));
}

// 更新金币信息
export function updateUserGoldCoin(userId: string, goldCoin: number, diffCoin: number) {
  if (PlatformSetting.isHTML5()) return;
  const url = `${uniConfig.apiUrl}/unigame/user/coin`;
  const xhr = new XMLHttpRequest();
  console.log('request url ', url, userId, goldCoin, diffCoin);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
      console.log(xhr.responseType, xhr.response, xhr.responseText);
      var response = JSON.parse(xhr.responseText);
    }
  };
  xhr.setRequestHeader("Content-Type", "application/json");
  // xhr.open("GET", url, true);
  xhr.open("POST", url, true);
  const data = {
    user_id: +userId,
    gold_coin: goldCoin,
    diff_coin: diffCoin,
  };
  xhr.send(JSON.stringify(data));
}

// 更新用户对局次数
export function updateUserGameNum(userId: string, diffNum: number, winNum: number, loseNum: number, tieNum: number) {
  if (PlatformSetting.isHTML5()) return;
  const url = `${uniConfig.apiUrl}/unigame/user/num`;
  const xhr = new XMLHttpRequest();
  console.log('request url ', url);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
      console.log(xhr.responseType, xhr.response, xhr.responseText);
      var response = JSON.parse(xhr.responseText);
    }
  };
  xhr.setRequestHeader("Content-Type", "application/json");
  // xhr.open("GET", url, true);
  xhr.open("POST", url, true);
  const data = {
    user_id: +userId,
    win_num: winNum,
    lose_num: loseNum,
    tie_num: tieNum,
    diff_num: diffNum,
  };
  xhr.send(JSON.stringify(data));
}

// 查询机器人用户列表信息
export function queryUserRankingList(userId: string, playLevel: number) {
  if (PlatformSetting.isHTML5()) {
    const dataBody = fakeRobotUserList();
    databus.updateUserRankingList(dataBody);
    return;
  }
  const url = `${uniConfig.apiUrl}/unigame/user/ranklist`;
  const xhr = new XMLHttpRequest();
  console.log('request url ', url);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
      // console.log(xhr.responseType, xhr.response, xhr.responseText);
      var response = JSON.parse(xhr.responseText);
      databus.updateUserRankingList(response);
    }
  };
  var gameName = "billiard";
  if (BYTEDANCE) {
    gameName = "bytedance_billiard";
  }
  xhr.open("GET", url + `?user_id=${userId}&page_size=100&page_index=1&game_name=${gameName}`, true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.send();
}

// 查询机器人用户列表信息
export function queryRobotUserList(userId: string, playLevel: number) {
  if (PlatformSetting.isHTML5()) {
    const dataBody = fakeRobotUserList();
    databus.updateRobotUserList(dataBody);
    return;
  }
  const url = `${uniConfig.apiUrl}/unigame/user/robots`;
  const xhr = new XMLHttpRequest();
  console.log('request url ', url);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
      // console.log(xhr.responseType, xhr.response, xhr.responseText);
      var response = JSON.parse(xhr.responseText);
      databus.updateRobotUserList(response);
    }
  };
  xhr.open("GET", url + `?user_id=${userId}&play_level=${playLevel}&page_size=100&page_index=1&game_name=billiard`, true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.send();
}

function fakeRobotUserList() {
  const body = {
    'userList': [
      { userId: '167890000', 'nickName': '无敌', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890001', 'nickName': '阳春', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890002', 'nickName': '白雪', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890003', 'nickName': '六月', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890004', 'nickName': '飞霜', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890005', 'nickName': '大年', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890006', 'nickName': '初一', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890007', 'nickName': '十五', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890008', 'nickName': '坑爹', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890009', 'nickName': '芹菜', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890010', 'nickName': '白菜', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890011', 'nickName': '西瓜', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890012', 'nickName': '美女', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890013', 'nickName': '好好学习', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890014', 'nickName': '天天向上', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890015', 'nickName': '十年寒窗', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890016', 'nickName': '三板斧', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890017', 'nickName': '你大爷', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890018', 'nickName': '李太岁', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890019', 'nickName': '独孤求败', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890020', 'nickName': '石浩', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
      { userId: '167890021', 'nickName': '唐三', 'avatarUrl': 'homeresource/default_user_header.png', 'playLevel': 1, 'goldCoin': 100, 'modifyTime': '', 'createTime': '' },
    ]
  };
  return body;
}